package com.tang.calculator;

import com.tang.calculator.annotation.Operate;
import com.tang.calculator.context.CalculateContext;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName: CalculateContext
 * @Author: freedu
 * @Date: 2021/3/19 11:26
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class CalculateFactory {

    @Autowired
    private Map<String, CalculateProcessor> map;

    public synchronized Context createContext(String billType, String operate){
        // 确定处理器
        CalculateProcessor processor = map.get(billType);
        Context context = new Context(billType, operate);
        context.setList(new ArrayList<>(map.values()));
        context.setProcessor(processor);
        return context;
    }

    public void run(Context context) {
        context.getProcessor().process(context);
    }
}
